home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 6 / FM Towns Free Software Collection 6.iso / ms_dos / iplink / iplink.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-08  |  19.1 KB  |  759 lines

  1. /*    PC98<->IBM ファイル転送 Copyright (c) E.Suto    , 1992-1993                */
  2. /*    修正履歴  Ver  0.00     1992/06/22     試作品                                    */
  3. /*              Ver  0.10     1992/06/23     近所公開版                                */
  4. /*              Ver  0.20     1992/06/26     簡易サーバーモード追加,共用ドライブ対応*/
  5. /*              Ver  0.21     1992/06/28     リモートコマンド追加                    */
  6. /*              Ver  0.30     1992/06/30     TIMEOUT()改造,オプション統合,速度表示    */
  7. /*              Ver  0.31     1992/07/03     簡易サーバーを出来るだけ止めないよう    */
  8. /*              Ver  0.32     1992/07/03     速度表示の0割り算ガード(超手抜き)    */
  9. /*              Ver  0.33     1992/07/06     . の消し方変更                            */
  10. /*              Ver  0.34     1992/07/10     受信ファイルがルートに行ってしまうバグ修正    */
  11. /*              Ver  0.35     1992/11/17     RS232Cのバグ修正(98版のみ)    */
  12. /*              Ver  0.36     1992/11/17     ちょっと高速化(^^;                        */
  13. /*              Ver  0.37     1992/11/18     タイマ値最適化                            */
  14. /*              Ver  0.38     1992/11/18     送信ファイル名バグ修正                    */
  15. /*              Ver  0.39     1992/11/18     サーバーバグ修正                        */
  16. /*              Ver  0.41     1993/02/04     inpのwaitを#defineに                    */
  17. /*              Ver  0.42     1993/02/04     ソースをちょっと統合(^^;                */
  18. /*              Ver  1.00     1993/ 2/ 3     失敗時にタイムアウトしない                */
  19. /*              Ver  1.01     1993/ 2/ 4     コマンド受信処理でタイムアウトしない    */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <dos.h>
  24. #include <io.h>
  25. #include <fcntl.h>
  26. #include <signal.h>
  27. #include <string.h>
  28. #include <time.h>
  29. #include <sys\timeb.h>
  30.  
  31. #define BUFFSIZE    32767
  32.  
  33. void    copyright() ;
  34. int        connect_r() ;
  35. int        connect_s() ;
  36. void    fp_command() ;
  37. void    fp_file() ;
  38. void    fp_send() ;
  39. void    fp_server() ;
  40. void    fp_receive0() ;    /* (^^;;;;    */
  41. void    fp_receive() ;
  42. int        receive( int , unsigned char * ) ;
  43. void    rs_init() ;
  44. int        send( int , unsigned char * ) ;
  45. void    sig_out() ;
  46. void    timeout() ;
  47. void    timeset() ;
  48. void    usage() ;
  49.  
  50. static    int                mode        ; /* 動作モード 0:S 1:R 2:サ 4:サR 5:コ    */
  51. static    unsigned char    name[128]    ; /* 操作ファイル名                        */
  52. static    unsigned char    path[128]    ; /* 送信先パス名                        */
  53. static    unsigned char    command[128]; /* 送信コマンド                        */
  54. static    unsigned char    buffer[BUFFSIZE];/* バッファ                        */
  55. struct    find_t            f_name        ; /* ワイルドカード展開データ            */
  56. struct    timeb            time_1        ; /* 速度計算用タイマ値1                */
  57. struct    timeb            time_2        ; /* 速度計算用タイマ値2                */
  58. static    long            timeout1    ; /* タイムアウト値                        */
  59. static    int                debug        ; /* 詳細表示                            */
  60.  
  61. /*---------------------------------------------------------------------------*/
  62.  
  63. /* 送信メイン処理 */
  64. void fp_send()
  65. {
  66.     int len , loop , i , err ;
  67.     unsigned long f_len ;
  68.     unsigned char *c , *c2 ;
  69.     unsigned char path_name[128] , full_name[128] ;
  70.     FILE *file ;
  71.     int handle ;
  72.     unsigned long speed , f_len_b ;
  73.  
  74. /* ファイル送信前フラグ送信(^^; */
  75.     buffer[0] = 0x02 ;
  76.     if( send( 1 , buffer ) ) {
  77.         if( mode == 2 ) return ;
  78.         timeout() ;
  79.     }
  80.     if( debug ) printf( "ファイル送信フラグ送信OK。\n" ) ;
  81.  
  82. /* ファイル送信フラグ送信(^^; */
  83.     buffer[0] = 0x00 ;
  84.     if( send( 1 , buffer ) ) {
  85.         if( mode == 2 ) return ;
  86.         timeout() ;
  87.     }
  88.     if( debug ) printf( "ファイル送信フラグ送信OK。\n" ) ;
  89.  
  90. /* 受信先パス名長送信 */
  91.     len = strlen( path ) ;
  92.     *buffer = (unsigned char)len ;
  93.     if( send( 1 , buffer ) ) {
  94.         if( mode == 2 ) return ;
  95.         timeout() ;
  96.     }
  97.     if( debug ) printf( "受信先パス名長送信OK。\n" ) ;
  98.  
  99. /* 受信先パス名送信 */
  100.     if( len ) {
  101.         if( send( len , path ) ) {
  102.             if( mode == 2 ) return ;
  103.             timeout() ;
  104.         }
  105.         if( debug ) printf( "受信先パス名送信OK。\n" ) ;
  106.     }
  107.  
  108. /* PATH名取り出し */
  109.     strcpy( path_name , name ) ;
  110.     if( ( c = strrchr( path_name , ':' ) ) == NULL ) {
  111.         c = path_name ;
  112.     }
  113.     if( ( c2 = strrchr( c , '\\' ) ) == NULL ) {
  114.         c2 = c ;
  115.     }
  116.     if( *c2 == ':' || *c2 == '\\' ) *(c2+1) = 0x00 ;
  117.     else strcpy( path_name , "" ) ;
  118.  
  119. /* ワイルドカード展開 */
  120.     if( ( _dos_findfirst( name , _A_ARCH|_A_RDONLY , &f_name ) ) != 0 ) {
  121.         printf( "送信出来るファイルがありません。\n" ) ;
  122.         buffer[0] = 0x00 ;
  123.         if( send( 1 , buffer ) ) {
  124.             if( mode == 2 ) return ;
  125.             timeout() ;
  126.         }
  127.         if( mode == 2 ) return ;
  128.         rs_init() ;
  129.         exit( -1 ) ;
  130.     }
  131. /* 送るデータ有り */
  132.     buffer[0] = 0xff ;
  133.     if( send( 1 , buffer ) ) {
  134.         if( mode == 2 ) return ;
  135.         timeout() ;
  136.     }
  137.  
  138. /* 送信処理ループ */
  139.     if( mode != 2 ) printf( " ファイル名    : ファイル長\n" ) ;
  140.     for( ; ; ) {
  141.         ftime( &time_1 ) ;
  142.         err = 0 ;
  143.  
  144. /* ファイルの長さを調べる _dos_finedではDIETしてあるデータの長さが分からない */
  145.         strcpy( full_name , path_name ) ;
  146.         strcat( full_name , f_name.name ) ;
  147.         if( ( handle = open( full_name , O_RDONLY|O_BINARY ) ) == EOF ) {
  148.             printf( "送信ファイルのオープンに失敗しました(%s)。\n" ,
  149.                 full_name ) ;
  150.             buffer[0] = 0x00 ;
  151.             if( send( 1 , buffer ) ) {
  152.                 if( mode == 2 ) return ;
  153.                 timeout() ;
  154.             }
  155.             if( mode == 2 ) return ;
  156.             rs_init() ;
  157.             exit( -1 ) ;
  158.         }
  159.         f_len = filelength( handle ) ;
  160.         f_len_b = f_len * 8l ;
  161.         close( handle ) ;
  162.  
  163. /* ファイルのオープン */
  164.         if( ( file = fopen( full_name , "rb" ) ) == NULL ) {
  165.             printf( "送信ファイルのオープンに失敗しました。\n" ) ;
  166.             buffer[0] = 0x00 ;
  167.             if( send( 1 , buffer ) ) {
  168.                 if( mode == 2 ) return ;
  169.                 timeout() ;
  170.             }
  171.             if( mode == 2 ) return ;
  172.             rs_init() ;
  173.             exit( -1 ) ;
  174.         }
  175.  
  176. /* 送信データのオープンOK */
  177.     buffer[0] = 0xff ;
  178.     if( send( 1 , buffer ) ) {
  179.         if( mode == 2 ) return ;
  180.         timeout() ;
  181.     }
  182.  
  183. /* ファイル名長送信 */
  184.         len = strlen( f_name.name ) ;
  185.         *buffer = (unsigned char)len ;
  186.         if( send( 1 , buffer ) ) {
  187.             if( mode == 2 ) return ;
  188.             timeout() ;
  189.         }
  190.         if( debug ) printf( "ファイル名長送信OK。\n" ) ;
  191.  
  192. /* ファイル名送信 */
  193.         strcpy( name , f_name.name ) ;
  194.         if( send( len , f_name.name ) ) {
  195.             if( mode == 2 ) return ;
  196.             timeout() ;
  197.         }
  198.         if( debug ) printf( "ファイル名送信OK。\n" ) ;
  199.  
  200. /* 受信側ファイルチェック */
  201.     if( receive( 1 , buffer ) ) {
  202.         if( mode == 2 ) return ;
  203.         timeout() ;
  204.     }
  205.     if( buffer[0] == 0x00 ) {
  206.         printf( "受信側でファイルのオープンに失敗しました。\n" ) ;
  207.         if( mode == 2 ) return ;
  208.         rs_init() ;
  209.         exit( -1 ) ;
  210.     }
  211.  
  212. /* ファイル長送信 */
  213.         buffer[0] = f_len / 0x1000000l ;
  214.         buffer[1] = ( f_len & 0x00ff0000l ) / 0x10000l ;
  215.         buffer[2] = ( f_len & 0x0000ff00l ) / 0x100l ;
  216.         buffer[3] = f_len & 0x000000ffl ;
  217.         if( send( 4 , buffer ) ) {
  218.             if( mode == 2 ) return ;
  219.             timeout() ;
  220.         }
  221.         if( debug ) printf( "ファイル長送信OK。\n" ) ;
  222.  
  223. /* ファイル日付送信 */
  224.         buffer[0] = f_name.wr_date / 0x100 ;
  225.         buffer[1] = f_name.wr_date ;
  226.         if( send( 2 , buffer ) ) {
  227.             if( mode == 2 ) return ;
  228.             timeout() ;
  229.         }
  230.         if( debug ) printf( "ファイル日付送信OK。\n" ) ;
  231.  
  232. /* ファイル時間送信 */
  233.         buffer[0] = f_name.wr_time / 0x100 ;
  234.         buffer[1] = f_name.wr_time ;
  235.         if( send( 2 , buffer ) ) {
  236.             if( mode == 2 ) return ;
  237.             timeout() ;
  238.         }
  239.         if( debug ) printf( "ファイル時間送信OK。\n" ) ;
  240.  
  241. /* ファイル属性送信 */
  242.         buffer[0] = f_name.attrib ;
  243.         if( send( 1 , buffer ) ) {
  244.             if( mode == 2 ) return ;
  245.             timeout() ;
  246.         }
  247.         if( debug ) printf( "ファイル属性送信OK。\n" ) ;
  248.  
  249. /* ファイル送信 */
  250.         printf( " %-13.13s : %10ld " , name , f_len ) ;
  251.         loop = ( f_len + BUFFSIZE - 1l ) / BUFFSIZE ;
  252.         for( i = 0 ; i < loop ; i++ ) putch( '.' ) ;
  253.         for( i = 0 ; i < loop ; i++ ) {
  254.             len = fread( buffer , 1 , BUFFSIZE , file ) ;
  255.             if( send( len , buffer ) ) {
  256.                 if( mode == 2 ) return ;
  257.                 timeout() ;
  258.             }
  259. /* 受信側ファイル書き込みチェック */
  260.             if( receive( 1 , buffer ) ) {
  261.                 if( mode == 2 ) return ;
  262.                 timeout() ;
  263.             }
  264.             if( buffer[0] == 0x00 ) {
  265.                 printf( "送信失敗!" ) ;
  266.                 err = 1 ;
  267.                 break ;
  268.             }
  269.             else printf( "\b \b" ) ;
  270.         }
  271.         if( !( err ) ) {
  272.             ftime( &time_2 ) ;
  273.             speed = ( f_len_b / ( ( time_2.time - time_1.time ) * 100l
  274.                   + time_2.millitm - time_1.millitm + 1l ) + 5l ) / 10l ;
  275.             printf( "(電送速度:約%ldkbps)" , speed ) ;
  276.         }
  277.  
  278. /* ファイルのクローズ */
  279.         fclose( file ) ;
  280.  
  281. /* 受信側ファイルチェック */
  282.     if( receive( 1 , buffer ) ) {
  283.         if( mode == 2 ) return ;
  284.         timeout() ;
  285.     }
  286.     if( buffer[0] == 0x00 ) {
  287.         printf( "×\n" ) ;
  288.     }
  289.     else printf( "○\n" ) ;
  290.  
  291. /* 次のファイル展開 */
  292.         if( ( _dos_findnext( &f_name ) ) != 0 ) {
  293. /* おしまい */
  294.             buffer[0] = 0x00 ;
  295.             if( send( 1 , buffer ) ) {
  296.                 if( mode == 2 ) return ;
  297.                 timeout() ;
  298.             }
  299.             break ;
  300.         }
  301. /* つづく */
  302.         else {
  303.             buffer[0] = 0xff ;
  304.             if( send( 1 , buffer ) ) {
  305.                 if( mode == 2 ) return ;
  306.                 timeout() ;
  307.             }
  308.             if( debug ) printf( "連続送信要求OK。\n" ) ;
  309.         }
  310.     }
  311.  
  312. }
  313.  
  314. /* 受信メイン前(^^;処理 */
  315. void fp_receive0()
  316. {
  317. /* ファイル送信前フラグ受信 */
  318.     if( receive( 1 , buffer ) ) timeout() ;
  319.     if( buffer[0] == 0x02 ) {
  320.         if( debug ) printf( "ファイル送信前フラグ受信OK。\n" ) ;
  321.     }
  322.     else {
  323.         printf( "ファイル送信前フラグが異常です、送信側コマンドを確認してください。\n" ) ;
  324.         rs_init() ;
  325.         exit( -1 ) ;
  326.     }
  327. }
  328.  
  329. /* 受信メイン処理 */
  330. void fp_receive()
  331. {
  332.     int i , j , len , loop , err ;
  333.     unsigned long f_len ;
  334.     unsigned int  f_time , f_date ;
  335.     unsigned char f_attr ;
  336.     unsigned char full_name[128] ;
  337.     FILE *file ;
  338.     int handle ;
  339.     unsigned long speed , f_len_b ;
  340.  
  341. /* ファイル送信フラグ受信 */
  342.     if( receive( 1 , buffer ) ) {
  343.         if( mode == 2 ) return ;
  344.         timeout() ;
  345.     }
  346.     if( buffer[0] == 0x00 ) {
  347.         if( debug ) printf( "ファイル送信フラグ受信OK。\n" ) ;
  348.     }
  349.     else {
  350.         printf( "ファイル送信フラグが異常です、送信側コマンドを確認してください。\n" ) ;
  351.         if( mode == 2 ) return ;
  352.         rs_init() ;
  353.         exit( -1 ) ;
  354.     }
  355.  
  356. /* 受信パス名長受信 */
  357.     if( receive( 1 , buffer ) ) {
  358.         if( mode == 2 ) return ;
  359.         timeout() ;
  360.     }
  361.     len = *buffer ;
  362.     if( debug ) printf( "受信パス名長受信OK。\n" ) ;
  363. /* 受信パス名受信 */
  364.     if( len ) {
  365.         if( receive( len , buffer ) ) {
  366.             if( mode == 2 ) return ;
  367.             timeout() ;
  368.         }
  369.         if( ( buffer[len-1] != '\\' ) && ( buffer[len-1] != ':' ) ) {
  370.             buffer[len] = '\\' ;
  371.             buffer[len+1] = 0x00 ;
  372.         }
  373.         else buffer[len] = 0x00 ;
  374.         if( debug ) printf( "受信パス名受信OK。\n" ) ;
  375.         strcpy( path , buffer ) ;
  376.     }
  377.     else strcpy( path , "" ) ;
  378.  
  379. /* 受信ファイルチェック */
  380.     if( receive( 1 , buffer ) ) {
  381.         if( mode == 2 ) return ;
  382.         timeout() ;
  383.     }
  384.     if( buffer[0] == 0x00 ) {
  385.         printf( "送信側に送るファイルがありません。\n" ) ;
  386.         if( mode == 2 ) return ;
  387.         rs_init() ;
  388.         exit( -1 ) ;
  389.     }
  390.  
  391. /* 受信処理ループ */
  392.     if( mode != 2 ) printf( " ファイル名    : ファイル長\n" ) ;
  393.     for( ; ; ) {
  394.         ftime( &time_1 ) ;
  395.         err = 0 ;
  396.  
  397. /* 受信ファイルチェック */
  398.     if( receive( 1 , buffer ) ) {
  399.         if( mode == 2 ) return ;
  400.         timeout() ;
  401.     }
  402.     if( buffer[0] == 0x00 ) {
  403.         printf( "送信側でファイルのオープンに失敗しました。\n" ) ;
  404.         if( mode == 2 ) return ;
  405.         rs_init() ;
  406.         exit( -1 ) ;
  407.     }
  408.  
  409. /* ファイル名長受信 */
  410.         if( receive( 1 , buffer ) ) {
  411.             if( mode == 2 ) return ;
  412.             timeout() ;
  413.         }
  414.         len = *buffer ;
  415.         if( debug ) printf( "ファイル名長受信OK。\n" ) ;
  416.  
  417. /* ファイル名受信 */
  418.         if( receive( len , buffer ) ) {
  419.             if( mode == 2 ) return ;
  420.             timeout() ;
  421.         }
  422.         buffer[len] = 0x00 ;
  423.         if( debug ) printf( "ファイル名受信OK。\n" ) ;
  424.         strcpy( name , buffer ) ;
  425.         strcpy( full_name , path ) ;
  426.         strcat( full_name , name ) ;
  427.  
  428. /* ファイルのオープン */
  429.         if( ( file = fopen( full_name , "wb" ) ) == NULL ) {
  430.             printf( "受信ファイルのオープンに失敗しました(%s)。\n" , full_name ) ;
  431.             buffer[0] = 0x00 ;
  432.             if( send( 1 , buffer ) ) {
  433.                 if( mode == 2 ) return ;
  434.                 timeout() ;
  435.             }
  436.             if( mode == 2 ) return ;
  437.             rs_init() ;
  438.             exit( -1 ) ;
  439.         }
  440.  
  441. /* オープン OK */
  442.     buffer[0] = 0xff ;
  443.     if( send( 1 , buffer ) ) {
  444.         if( mode == 2 ) return ;
  445.         timeout() ;
  446.     }
  447.  
  448. /* ファイル長受信 */
  449.         if( receive( 4 , buffer ) ) {
  450.             if( mode == 2 ) return ;
  451.             timeout() ;
  452.         }
  453.         f_len = (unsigned long)buffer[0] * 0x1000000l
  454.             + (unsigned long)buffer[1] * 0x10000l
  455.             + (unsigned long)buffer[2] * 0x100l
  456.             + (unsigned long)buffer[3] ;
  457.         f_len_b = f_len * 8l ;
  458.         if( debug ) printf( "ファイル長受信OK。\n" ) ;
  459.  
  460. /* ファイル日付受信 */
  461.         if( receive( 2 , buffer ) ) {
  462.             if( mode == 2 ) return ;
  463.             timeout() ;
  464.         }
  465.         f_date = (unsigned int)buffer[0] * 0x100
  466.             + (unsigned int)buffer[1] ;
  467.         if( debug ) printf( "ファイル日付受信OK。\n" ) ;
  468.  
  469. /* ファイル時間受信 */
  470.         if( receive( 2 , buffer ) ) {
  471.             if( mode == 2 ) return ;
  472.             timeout() ;
  473.         }
  474.         f_time = (unsigned int)buffer[0] * 0x100
  475.             + (unsigned int)buffer[1] ;
  476.         if( debug ) printf( "ファイル時間受信OK。\n" ) ;
  477.  
  478. /* ファイル属性受信 */
  479.         if( receive( 1 , buffer ) ) {
  480.             if( mode == 2 ) return ;
  481.             timeout() ;
  482.         }
  483.         f_attr = buffer[0] ;
  484.         if( debug ) printf( "ファイル属性受信OK。\n" ) ;
  485.  
  486. /* ファイル受信 */
  487.         printf( " %-13.13s : %10ld " , name , f_len ) ;
  488.         loop = ( f_len + BUFFSIZE - 1l ) / BUFFSIZE ;
  489.         for( i = 0 ; i < loop ; i++ ) putch( '.' ) ;
  490.         for( i = 0 ; i < loop ; i++ ) {
  491.             if( f_len < BUFFSIZE ) j = f_len ;
  492.             else j = BUFFSIZE ;
  493.             if( receive( j , buffer ) ) {
  494.                 if( mode == 2 ) return ;
  495.                 timeout() ;
  496.             }
  497.             if( fwrite( buffer , 1 , j    , file ) < j ) {
  498.                 printf( "書き込み失敗!" ) ;
  499.                 err = 1 ;
  500.                 buffer[0] = 0x00 ;
  501.                 if( send( 1 , buffer ) ) {
  502.                     if( mode == 2 ) return ;
  503.                     timeout() ;
  504.                 }
  505.                 break ;
  506.             }
  507.             printf( "\b \b" ) ;
  508.             f_len -= (unsigned long)j ;
  509.             buffer[0] = 0xff ;
  510.             if( send( 1 , buffer ) ) {
  511.                 if( mode == 2 ) return ;
  512.                 timeout() ;
  513.             }
  514.         }
  515.         if( !( err ) ) {
  516.             ftime( &time_2 ) ;
  517.             speed = ( f_len_b / ( ( time_2.time - time_1.time ) * 100l
  518.                   + time_2.millitm - time_1.millitm + 1l ) + 5l ) / 10l ;
  519.             printf( "(電送速度:約%ldkbps)" , speed ) ;
  520.         }
  521.  
  522. /* ファイルのクローズ */
  523.         fclose( file ) ;
  524.  
  525. /* ファイルの日時属性設定 */
  526.         if( !( err ) ) {
  527.             if( _dos_open( full_name , O_WRONLY , &handle ) != 0x00 ) {
  528.                 printf( "受信ファイルのオープンに失敗しました。" ) ;
  529.                 err = 2 ;
  530.             }
  531.             if( _dos_setftime( handle , f_date , f_time ) != 0x00 ) {
  532.                 printf( "受信ファイルの日時設定に失敗しました。" ) ;
  533.                 err = 3 ;
  534.             }
  535.             if( _dos_setfileattr( full_name , f_attr ) != 0x00 ) {
  536.                 printf( "送信ファイルの属性設定に失敗しました。" ) ;
  537.                 err = 3 ;
  538.             }
  539.             if( err != 2 ) _dos_close( handle ) ;
  540.         }
  541.  
  542. /* 受信 NG */
  543.         if( err ) {
  544.             printf( "×\n" ) ;
  545.             unlink( full_name ) ;
  546.             buffer[0] = 0x00 ;
  547.             if( send( 1 , buffer ) ) {
  548.                 if( mode == 2 ) return ;
  549.                 timeout() ;
  550.             }
  551.         }
  552. /* 受信 OK */
  553.         else {
  554.             printf( "○\n" ) ;
  555.             buffer[0] = 0xff ;
  556.             if( send( 1 , buffer ) ) {
  557.                 if( mode == 2 ) return ;
  558.                 timeout() ;
  559.             }
  560.         }
  561.  
  562. /* 次のファイルチェック */
  563.         if( receive( 1 , buffer ) ) {
  564.             if( mode == 2 ) return ;
  565.             timeout() ;
  566.         }
  567.         if( buffer[0] == 0x00 ) break ;
  568.         if( debug ) printf( "連続受信要求OK。\n" ) ;
  569.  
  570.     }
  571.  
  572. }
  573.  
  574. /* サーバーメイン処理 */
  575. void fp_server()
  576. {
  577.     int len ;
  578.     unsigned long f_len ;
  579.     int handle ;
  580.     FILE *file ;
  581.  
  582. /* ESCが来るまで無限ループ */
  583.     printf( " ファイル名    : ファイル長\n" ) ;
  584.     for( ; ; ) {
  585.         strcpy( path , "" ) ;
  586.         while( connect_r() ) ;
  587.  
  588. /* ファイル送信フラグ受信 */
  589.         if( receive( 1 , buffer ) ) buffer[0] = 0xff ;
  590.         switch( buffer[0] ) {
  591.             case 0x01 :            /* サーバーから送信 */
  592. /* 送信ファイル名長受信 */
  593.                 if( receive( 1 , buffer ) ) break ;
  594.                 len = *buffer ;
  595.                 if( debug ) printf( "送信ファイル名長受信OK。\n" ) ;
  596. /* 送信ファイル名受信 */
  597.                 if( receive( len , buffer ) ) break ;
  598.                 buffer[len] = 0x00 ;
  599.                 if( debug ) printf( "送信ファイル名受信OK。\n" ) ;
  600.                 strcpy( name , buffer ) ;
  601.                 fp_send() ;
  602.                 break ;
  603.             case 0x02 :            /* サーバーに受信    */
  604.                 fp_receive() ;
  605.                 break ;
  606.             case 0x03 :            /* リモートコマンド処理 */
  607. /* リモートコマンド長受信 */
  608.                 if( receive( 1 , buffer ) ) break ;
  609.                 len = *buffer ;
  610.                 if( debug ) printf( "リモートコマンド長受信OK。\n" ) ;
  611. /* リモートコマンド受信 */
  612.                 if( receive( len , command ) ) break ;
  613.                 command[len] = 0x00 ;
  614.                 if( debug ) printf( "リモートコマンド受信OK。\n" ) ;
  615. /* リモートコマンド実行 */
  616.                 strcat( command , ">$$fpli$$.tmp" ) ;
  617.                 system( command ) ;
  618. /* ファイルの長さを調べる _dos_finedではDIETしてあるデータの長さが分からない */
  619.                 strcpy( name , "$$fpli$$.tmp" ) ;
  620.                 if( ( handle = open( name , O_RDONLY|O_BINARY ) ) == EOF ) {
  621.                     f_len = 0l ;
  622.                 }
  623.                 else {
  624.                     f_len = filelength( handle ) ;
  625.                     close( handle ) ;
  626.                 }
  627. /* コマンド結果ファイルのオープン */
  628.                 file = fopen( name , "rb" ) ;
  629. /* コマンド結果ファイル長送信 */
  630.                 buffer[0] = f_len / 0x1000000l ;
  631.                 buffer[1] = ( f_len & 0x00ff0000l ) / 0x10000l ;
  632.                 buffer[2] = ( f_len & 0x0000ff00l ) / 0x100l ;
  633.                 buffer[3] = f_len & 0x000000ffl ;
  634.                 if( send( 4 , buffer ) ) break ;
  635.                 if( debug ) printf( "コマンド結果ファイル長送信OK。\n" ) ;
  636. /* コマンド結果ファイル送信 */
  637.                 for( ; f_len > 0l ; ) {
  638.                     len = fread( buffer , 1 , BUFFSIZE , file ) ;
  639.                     if( send( len , buffer ) ) break ;
  640.                     f_len -= (unsigned long)len ;
  641.                 }
  642.                 if( debug ) printf( "コマンド結果ファイル送信OK。\n" ) ;
  643.                 fclose( file ) ;
  644.                 unlink( name ) ;
  645.                 break ;
  646.             default :
  647.                 printf( "ファイル送信フラグが異常です、送信側コマンドを確認してください。\n" ) ;
  648.                 break ;
  649.         }
  650.     if( debug ) printf( "サーバー処理1シーケンス完了\n" ) ;
  651.     }
  652.  
  653. }
  654.  
  655. /* サーバーからの受信処理用(^^;ファイル名送信処理 */
  656. void fp_file()
  657. {
  658.     int len ;
  659.  
  660. /* ファイル名送信フラグ送信(^^; */
  661.     buffer[0] = 0x01 ;
  662.     if( send( 1 , buffer ) ) timeout() ;
  663.     if( debug ) printf( "ファイル名送信フラグ送信OK。\n" ) ;
  664.  
  665. /* 受信ファイル名長送信 */
  666.     len = strlen( name ) ;
  667.     *buffer = (unsigned char)len ;
  668.     if( send( 1 , buffer ) ) timeout() ;
  669.     if( debug ) printf( "受信ファイル名長送信OK。\n" ) ;
  670.  
  671. /* 受信ファイル名送信 */
  672.     if( send( len , name ) ) timeout() ;
  673.     if( debug ) printf( "受信ファイル名送信OK。\n" ) ;
  674.  
  675. }
  676.  
  677. /* リモートコマンド処理 */
  678. void fp_command()
  679. {
  680.     int len , i , j;
  681.     unsigned long f_len ;
  682.     unsigned char *c ;
  683.  
  684. /* リモートコマンド送信フラグ送信(^^; */
  685.     buffer[0] = 0x03 ;
  686.     if( send( 1 , buffer ) ) timeout() ;
  687.     if( debug ) printf( "リモートコマンド送信フラグ送信OK。\n" ) ;
  688.  
  689. /* リモートコマンド長送信 */
  690.     len = strlen( command ) ;
  691.     *buffer = (unsigned char)len ;
  692.     if( send( 1 , buffer ) ) timeout() ;
  693.     if( debug ) printf( "リモートコマンド長送信OK。\n" ) ;
  694.  
  695. /* リモートコマンド送信 */
  696.     if( send( len , command ) ) timeout() ;
  697.     if( debug ) printf( "受信先パス名送信OK(%s)。\n" , command ) ;
  698.  
  699. /* コマンド結果長受信 */
  700.     while( receive( 4 , buffer ) ) ;
  701.     f_len = (unsigned long)buffer[0] * 0x1000000l
  702.         + (unsigned long)buffer[1] * 0x10000l
  703.         + (unsigned long)buffer[2] * 0x100l
  704.         + (unsigned long)buffer[3] ;
  705.     if( debug ) printf( "コマンド結果長受信OK。\n" ) ;
  706.  
  707. /* コマンド結果受信&表示 */
  708.     for( ; f_len > 0l ; ) {
  709.         if( f_len < BUFFSIZE ) i = f_len ;
  710.         else i = BUFFSIZE ;
  711.         if( receive( i , buffer ) ) timeout() ;
  712.         c = buffer ;
  713.         for( j = 0 ; j < i ; j++ ) {
  714.             putch( *c++ ) ;
  715.         }
  716.         f_len -= (unsigned long)i ;
  717.     }
  718.     if( debug ) printf( "コマンド結果受信OK。\n" ) ;
  719.  
  720. }
  721.  
  722. /* タイムアウト停止 */
  723. void timeout()
  724. {
  725.     printf( "タイムアウトが発生しました(;_;)。\n" ) ;
  726.     rs_init() ;
  727.     exit( -1 ) ;
  728.  
  729. }
  730.  
  731. /* 割り込み処理定義 */
  732. void sig_out()
  733. {
  734.  
  735.     printf( "電送処理終了します。\n" ) ;
  736.     rs_init() ;
  737.     exit( -1 ) ;
  738.  
  739. }
  740.  
  741. /* タイムアウト値取得処理 */
  742. void timeset()
  743. {
  744.     long time_0,time_1,time_2 ;
  745.  
  746.     time_0 = clock() ;
  747.     time_1 = time_0 + 1l ;
  748.     time_2 = time_0 + 2l ;
  749.  
  750.     while( clock() != time_1 ) ;
  751.     for( timeout1 = 0l ; timeout1 < 2000000000l ; timeout1 += 2500l ) {
  752.         if( clock() == time_2 ) break ;
  753.     }
  754.  
  755.     if( debug ) printf( "タイムアウト値は(%ld)です。\n" , timeout1 ) ; 
  756.  
  757. }
  758.  
  759.